//import Check.js
$import('Check.js', 'js');
$import('xmlhttp.js', 'js');

function $import(path, type){
	var i, 
	  base, 
	  src = "COMMON.js", 
	  scripts = document.getElementsByTagName("script"); 

	for (i = 0; i < scripts.length; i++) {
	  if (scripts[i].src.match(src)) {
		  base = scripts[i].src.replace(src, "");
		  break;
	  }
	}
	
	if (type == "css") {
	  document.write("<" + "link href=\"" + base + path + "\" rel=\"stylesheet\" type=\"text/css\"></" + "link>");
	} else {
	  document.write("<" + "script src=\"" + base + path + "\"></" + "script>");
	}
}

function addSetMethod(obj,prop){
	var temp = (prop.substring(0,1).toUpperCase())+prop.substring(1);
	eval(obj+".prototype.set"+temp+" = function("+prop+"){if(!isNull("+prop+")){this."+prop+" = "+prop+"}}");
}

function isNull(str){
	return str==null||str=="null";
}

function Tree(){
	this.treeType = null;
	this.rootId = null;
	this.checkedId = null;
	this.selectType = null;
	this.selectAll = null;
	this.width = null;
	this.height = null;
	this.queryString = null;
	this.treeName = null;
	this.showParent = null;
	this.params = null;
}

addSetMethod("Tree","treeType");
addSetMethod("Tree","rootId");
addSetMethod("Tree","checkedId");
addSetMethod("Tree","selectType");
addSetMethod("Tree","selectAll");

addSetMethod("Tree","width");
addSetMethod("Tree","height");
addSetMethod("Tree","queryString");
addSetMethod("Tree","treeName");
addSetMethod("Tree","showParent");

addSetMethod("Tree","params");

function selectTree(tree){
	if(tree.treeType==null){
		alert("Error treeType is required!");
		return false;
	}
	
	if(tree.height==null)
		tree.height = 370;
	if(tree.width==null)
		tree.width = 400;
		
	var Y=(window.screen.height-tree.height)/2;
    var X=(window.screen.width-tree.width)/2;
    var dm="dialogHeight:"+tree.height+"px;dialogWidth:"+tree.width+"px;dialogLeft:"+X+"px;dialogTop:"+Y+"px;center:yes;resizable:no;status:no;";
	var url = globalContextPath+"/common/selectTree.jsp?time="+((new Date()).getTime());
	if(tree.treeType!=null)
		url += "&treeType="+tree.treeType;
	if(tree.selectType!=null)
		url += "&selectType="+tree.selectType;
	if(tree.selectAll!=null)
		url += "&selectAll="+tree.selectAll;	
	if(tree.rootId!=null)
		url += "&rootId="+tree.rootId;
	if(tree.checkedId!=null)
		url += "&checkedId="+tree.checkedId;	
	if(tree.params!=null){
		for(var i=0;i<tree.params.length;i++){
			url += "&param="+tree.params[i];
		}
	}
	if(tree.treeName!=null){
		url += "&treeName="+tree.treeName;
	}
	if(tree.queryString!=null)
		url += "&queryString="+tree.queryString;	
	if(tree.showParent!=null)
		url += "&showParent="+tree.showParent;		
	//alert(url);
	var retv=window.showModalDialog(url, "" ,dm);
	return retv;
	if(!retv) 
		return null;
	else
		return retv;
}

function getValues(tree){
	var retv = selectTree(tree);
	if(retv==null||retv=="undefined"){
		return null;
	}
	if(retv==""){
		return null;
	}else{
		var ids = "";
		var names = "";
		var arr = retv.split(";");
		for(var i=0;i<arr.length;i++){
			var temp = arr[i];
			if(i!=0){
				ids += ",";
				names += ",";
			}
			ids += temp.substring(0,temp.indexOf(","));
			names += temp.substring(temp.indexOf(",")+1);
		}
		return new Array(ids,names);
	}
}